package dev.zeno.booru.db;

import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BaseDBAdapter {

	private Context context;
	private SQLiteDatabase database;
	private BaseDBHelper helper;
	private String name;
	private Map<String, String> types;
	
	public BaseDBAdapter (Context context, String name, Map<String, String> types)
	{
		this.context = context;
		this.name = name;
		this.types = types;
	}
	
	public BaseDBAdapter open() throws SQLException
	{
		helper = new BaseDBHelper(context, name, types);
		database = helper.getWritableDatabase();
		return this;
	}
	public void close()
	{
		helper.close();
	}
	public long updateItem(Map<String, String> where, Map<String, Object> params)
	{
		ContentValues values = createContentValues(params);
		String whereString = "";
		String[] whereValues = new String[where.size()];
		int index = 0;
		for(String w : where.keySet())
		{
			whereValues[index++] = where.get(w);
			whereString += w + "=?";
			if(index < whereValues.length - 1)
				whereString += " AND ";
		}
		return database.update(name, values, whereString, whereValues);
	}
	public long createItem(Map<String, Object> params)
	{
		ContentValues values = createContentValues(params);
		return database.insert(name, null, values);
	}
	public Cursor fetchAll()
	{
		String[] columns = new String[types.keySet().size()];
		types.keySet().toArray(columns);
		Cursor mCursor =  database.query(name, columns,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	public Cursor fetch(Map<String, String> where)
	{
		String[] columns = new String[types.keySet().size()];
		types.keySet().toArray(columns);
		
		String whereString = "";
		String[] whereValues = new String[where.size()];
		int index = 0;
		for(String w : where.keySet())
		{
			whereValues[index++] = where.get(w);
			whereString += w + "=?";
			if(index < whereValues.length - 1)
				whereString += " AND ";
		}
		
		Cursor mCursor =  database.query(name, columns,
				whereString, whereValues, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	public long delete(Map<String, String> where)
	{
		
		String whereString = "";
		String[] whereValues = new String[where.size()];
		int index = 0;
		for(String w : where.keySet())
		{
			whereValues[index++] = where.get(w);
			whereString += w + "=?";
			if(index < whereValues.length - 1)
				whereString += " AND ";
		}
		return database.delete(name, whereString, whereValues);
	}
	private ContentValues createContentValues(Map<String, Object> params) {
		ContentValues values = new ContentValues();
		
		for(String paramName : params.keySet())
		{
			Object obj = params.get(paramName);
			if(obj instanceof Boolean)
				values.put(paramName, (Boolean) obj);
			else if (obj instanceof Byte)
				values.put(paramName, (Byte) obj);
			else if (obj instanceof byte[])
				values.put(paramName, (byte[]) obj);
			else if (obj instanceof Double)
				values.put(paramName, (Double) obj);
			else if (obj instanceof Float)
				values.put(paramName, (Float) obj);
			else if (obj instanceof Integer)
				values.put(paramName, (Integer) obj);
			else if (obj instanceof Long)
				values.put(paramName, (Long) obj);
			else if (obj instanceof Short)
				values.put(paramName, (Short) obj);
			else if (obj instanceof String)
				values.put(paramName, (String) obj);
			else
				values.putNull(paramName);
		}
		
		return values;
	}
}

class BaseDBHelper extends SQLiteOpenHelper
{
	
	private String createTable;
	private static final int DATABASE_VERSION = 1;

	public BaseDBHelper(Context context, String name, Map<String, String> types) {
		super(context, name, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
		
		StringBuilder params = new StringBuilder();
		int i = 0;
		
		for(String paramName: types.keySet())
		{
			params.append(paramName + " " + types.get(paramName));
			if(i < types.size() - 1)
				params.append(", ");
			i++;
		}
		createTable = "CREATE TABLE " + name + " (" + params.toString() + ");";
		//Log.d("Constructor", createTable);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//Log.d("Create Table", createTable);
		db.execSQL(createTable);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub	
	}
}
